var QQMapWX = require('../../../utils/qqmap-wx-jssdk');
var WxNotificationCenter = require('../../../utils/WxNotificationCenter');
var that;
var qqmapsdk;

Page({
    onLoad: function (options) {
        that = this;
        qqmapsdk = new QQMapWX({
            key: 'BJFBZ-ZFTHW-Y2HRO-RL2UZ-M6EC3-GMF4U'
        });
        that.reloadCurrent();
    },

    keywordTyping: function (e) {
        var keyword = e.detail.value;
        qqmapsdk.getSuggestion({
            keyword: keyword,
            region: that.data.city,
            success: function (res) {
                that.setData({
                    result: res.data
                });
            },
            fail: function(res) {
                console.log(res);
            },
            complete: function(res) {
                console.log(res);
            }
        });
    },

    addressTapped: function (e) {
        var title = e.currentTarget.dataset.title;
        WxNotificationCenter.postNotificationName("poiSelectedNotification", title);
        wx.navigateBack();
    },

    geoTapped: function () {
        var title = that.data.address;
        WxNotificationCenter.postNotificationName("poiSelectedNotification", title);
        wx.navigateBack();
    },

    reloadCurrent: function () {
        that.setData({
            address: '正在定位中...',
        });
        qqmapsdk.reverseGeocoder({
            poi_options: 'policy=2',
            get_poi: 1,
            success: function(res) {
                that.setData({
                    address: res.result.formatted_addresses.recommend,
                    result: res.result.pois,
                    city: res.result.address_component.city
                });
            },
            fail: function(res) {
                console.log(res);
            },
            complete: function(res) {
                console.log(res);
            }
        });
    },

    selectLocation: function() {
        var that = this;
        wx.getLocation({
            type: 'gcj02',
            success: function (res) {
                var latitude = res.latitude;
                var longitude = res.longitude;

                wx.chooseLocation({
                    latitude: latitude,
                    longitude: longitude,
                    success: function(res) {
                        that.setData({
                            latitude: res.latitude,
                            longitude: res.longitude,
                            address: res.address
                        });
                        that.getWeather();
                        that.getCityByLocation();
                    },
                    fail: function(err) {
                        console.log('选择位置失败：', err);
                    }
                });
            },
            fail: function(err) {
                console.log('获取位置失败：', err);
                wx.showModal({
                    title: '获取地图位置失败',
                    content: '为了给您提供准确的天气预报服务,请在设置中授权【位置信息】',
                    success: function (mRes) {
                        if (mRes.confirm) {
                            wx.openSetting({
                                success: function (data) {
                                    if (data.authSetting["scope.userLocation"] === true) {
                                        that.selectLocation();
                                    } else {
                                        wx.showToast({
                                            title: '授权失败',
                                            icon: 'none',
                                            duration: 1000
                                        });
                                    }
                                },
                                fail: function(err) {
                                    console.log(err);
                                    wx.showToast({
                                        title: '唤起设置页失败，请手动打开',
                                        icon: 'none',
                                        duration: 1000
                                    });
                                }
                            });
                        }
                    }
                });
            }
        });
    },

    getWeather: function() {
        // 实现获取天气信息的逻辑
    },

    getCityByLocation: function() {
        // 实现通过位置获取城市信息的逻辑
    }
});
